package com.funong.matrix.biz.base.dao;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funong.matrix.biz.base.entity.SysMenuEntity;
import com.funong.matrix.biz.base.mapper.SysMenuMapper;
import com.funong.matrix.core.base.Dao;
import com.funong.matrix.core.base.FunongBaseDao;
import com.funong.matrix.core.dto.QueryDTO;
import com.funong.matrix.core.enums.IsDeletedEnum;
import com.funong.matrix.core.enums.StatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Aspect;

import java.util.List;

/**
 * @author : NaNa
 * @since : 2023-05-19 16:40
 **/
@Aspect
@Dao
public class SysMenuDao extends FunongBaseDao<SysMenuMapper, SysMenuEntity> {
    /**
     * 菜单分页查询
     *
     * @param queryDTO 查询条件
     * @return 菜单分页结果
     */
    public IPage<SysMenuEntity> queryByCondition(QueryDTO<SysMenuEntity> queryDTO) {
        SysMenuEntity menuEntity = queryDTO.getCondition();
        return this.page(new Page<>(queryDTO.getPageNo(), queryDTO.getPageSize()), new LambdaQueryWrapper<SysMenuEntity>()
                .like(null != menuEntity && StringUtils.isNotBlank(menuEntity.getName()), SysMenuEntity::getName, menuEntity.getName())
                .eq(null != menuEntity && null != menuEntity.getStatus(), SysMenuEntity::getStatus, menuEntity.getStatus())
                .eq(null != menuEntity && null != menuEntity.getSystemId(), SysMenuEntity::getSystemId, menuEntity.getSystemId())
                .eq(SysMenuEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue()));
    }

    public List<SysMenuEntity> getMenuListByCodeList(List<String> menuCodeList) {
        return this.list(new LambdaQueryWrapper<SysMenuEntity>()
                .in(!CollectionUtils.isEmpty(menuCodeList), SysMenuEntity::getCode, menuCodeList)
                .eq(SysMenuEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue()));
    }

    public SysMenuEntity getMenuByCode(String code) {
        List<SysMenuEntity> menuEntityList = this.list(new LambdaQueryWrapper<SysMenuEntity>()
                .eq(StringUtils.isNotBlank(code), SysMenuEntity::getCode, code)
                .eq(SysMenuEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue()));
        return CollectionUtils.isEmpty(menuEntityList) ? null : menuEntityList.get(0);
    }

    public List<SysMenuEntity> getMenuListBySystem(Integer systemId) {
        return this.list(new LambdaQueryWrapper<SysMenuEntity>()
                .eq(SysMenuEntity::getStatus, StatusEnum.ENABLE.getValue())
                .eq(null != systemId, SysMenuEntity::getSystemId, systemId)
                .eq(SysMenuEntity::getIsDeleted, IsDeletedEnum.NOT_DELETED.getValue()));
    }

    /**
     * 下拉选择
     *
     * @param systemId
     * @return
     */
    public List<Tree<String>> querySelect(Integer systemId) {
        List<SysMenuEntity> list = this.list(
                SysMenuEntity.gw()
                        .eq(SysMenuEntity::getSystemId, systemId)
                        .select(SysMenuEntity::getCode, SysMenuEntity::getName, SysMenuEntity::getParentCode)
        );
        return TreeUtil.build(list, "0", (item, treeNode) -> treeNode.setParentId(item.getParentCode()).setId(item.getCode()).setName(item.getName()));
    }
}
